package com.fs.user.web;

import com.fs.entity.Page;
import com.fs.entity.User;
import com.fs.entity.UserCondition;
import com.fs.service.UserService;
import com.fs.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/ListServlet")
public class QueryByPageUserServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       //判断是否登录
        if(request.getSession().getAttribute("login") == null){ //没有登录
            //跳转到登录页面
            response.sendRedirect("/login.jsp");
            return;
        }


        //2. 获取请求参数
        //页码
        String pageIndexStr = request.getParameter("pageIndex");
        //进行数据类型转换
        int pageIndex = 1; //默认第一页
        //如果前端传递页码
        if(pageIndexStr != null &&  !pageIndexStr.isEmpty()){
            //把前端传递的页码进行数据类型转换,赋值给pageIndex
            pageIndex =Integer.parseInt(pageIndexStr);
        }

        //页容量
        String pageSizeStr = request.getParameter("pageSize");
        //进行数据类型转换
        int pageSize = 5; //默认页容量5
        //如果前端传递页码
        if(pageSizeStr != null &&  !pageSizeStr.isEmpty()){
            //把前端传递的页码进行数据类型转换,赋值给pageIndex
            pageSize =Integer.parseInt(pageSizeStr);
        }
        //=========条件=======

        String name = request.getParameter("name");
        if(name != null){
          name =   name.trim();
        }
        String address = request.getParameter("address");
        if(address != null){
            address =   address.trim();
        }
        String startAgeStr = request.getParameter("startAge");
        if(startAgeStr != null){
            startAgeStr =   startAgeStr.trim();
        }
        Integer startAge = null;
        if(!(startAgeStr == null ||startAgeStr.isEmpty()) ){
            startAge = Integer.parseInt(startAgeStr);
        }

        String endAgeStr = request.getParameter("endAge");
        if(endAgeStr != null){
            endAgeStr =   endAgeStr.trim();
        }
        Integer endAge = null;
        if(!(endAgeStr == null ||endAgeStr.isEmpty()) ){
            endAge = Integer.parseInt(endAgeStr);
        }
        //把参数封装成条件对象
        UserCondition userCondition = new UserCondition(name,address,startAge,endAge);

        //================
        //3.调用业务层类的方法
        UserService userService = new UserServiceImpl();
        //Page<User> page =  userService.queryByPage(pageIndex,pageSize);

        Page<User> page =  userService.queryPageByCondition(pageIndex,pageSize,userCondition);
        //把数据存放在 request域
        request.setAttribute("page",page);
        //把条件对象保存到request域
        request.setAttribute("condition",userCondition);

        //4.根据3的结果 跳转页面  请求转发
        //list.html 变成list.jsp   html不是java技术,无法访问域, 变成jsp
        request.getRequestDispatcher("/pages/list.jsp").forward(request,response);
    }
}
